package com.example.shop_2208.fragment

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.example.shop_2208.entity.AddCarEntityResult
import com.example.shop_2208.entity.CarEntityResult
import com.example.shop_2208.entity.ParamAddcar
import com.example.shop_2208.entity.ParamDeleteCar
import com.example.shop_2208.mvvm.BaseViewModel
import kotlinx.coroutines.launch

class CarViewModel :BaseViewModel(){

    val selectSuccess = MutableLiveData<CarEntityResult>()
    val selectFail = MutableLiveData<String>()

    fun selectCar(){
        viewModelScope.launch {
            try {
                selectSuccess.value = apiServer.selectCar()
            }catch (e:Exception){
                selectFail.value = e.message
            }
        }
    }

    val addSuccess = MutableLiveData<AddCarEntityResult>()
    val addFail = MutableLiveData<String>()
    fun addCar(goods_id: Int,count:Int){
        viewModelScope.launch {
            try {
                val param = ParamAddcar(count, goods_id)
                addSuccess.value = apiServer.addCar(param)
            }catch (e:Exception){
                addFail.value = e.message
            }
        }

    }

    val deleteSuccess = MutableLiveData<AddCarEntityResult>()
    val deleteFail = MutableLiveData<String>()
    fun deleteCar(ids:String){
        viewModelScope.launch {
            try {
                val param = ParamDeleteCar(ids)
                deleteSuccess.value = apiServer.deleteCar(param)
            }catch (e:Exception){
                deleteFail.value = e.message
            }
        }
    }
}